<?php
/**
 * 文章类信息控制器
 *
 * @package    app
 * @version    1.0
 * @author     Ray 33705910@qq.com
 * @license    MIT License
 * @copyright  2013 - 2015 Ray
 * @link       http://wangxiaolei.cn
 */

/**
 * 本控制器主要用于：
 * 1.
 * @package  app
 * @extends  Controller
 */
namespace admin;

class Controller_Article extends Controller_BaseController {


    public function before(){
        parent::before();

        $params = array(
            'controller_name' => '文章管理'
        );
        \View::set_global($params);
    }

    /**
    * 文章列表
    *
    * @param $cid 文章类目ID
    * @return
    */
    public function action_index(){
        $params = array(
            'title' => '文章列表',
            'menu' => 'acticler',
        );

        switch (\Input::get('action', 'article')) {
            case 'article':
                $params['controller_name'] = '文章管理';
                $params['action_name'] = '文章列表';
                $params['title'] = '文章列表';
                $params['menu'] = 'article';
                break;
            case 'page':
                $params['controller_name'] = '页面管理';
                $params['action_name'] = '单页列表';
                $params['title'] = '单页列表';
                $params['menu'] = 'page';
                break;
            case 'case':
                $params['controller_name'] = '案例管理';
                $params['action_name'] = '案例列表';
                $params['title'] = '案例列表';
                $params['menu'] = 'case';
                break;
            case 'ad':
                $params['controller_name'] = '广告管理';
                $params['action_name'] = '广告列表';
                $params['title'] = '广告列表';
                $params['menu'] = 'ad';
                break;
        }

        $params_string = '?';

        if(\Input::get('action', false) == 'page'){
            $ids = array(\Session::get('seller')->page_category);
            $params_string = 'action=page';
        }else if(\Input::get('action', false) == 'case'){
            $ids = \Model_Category::getChildIds(\Session::get('seller')->case_category);
            $params_string = 'action=case';
        }else if(\Input::get('action', false) == 'ad'){
            $ids = \Model_Category::getChildIds(\Session::get('seller')->ad_category);
            $params_string = 'action=ad';
        }else if(\Input::get('category_id', false)){
            $ids = \Model_Category::getChildIds(\Input::get('category_id'));
            array_push($ids, \Input::get('category_id'));
            $params_string = 'category_id=' . \Input::get('category_id');
        }else{
            $ids = \Model_Category::getChildIds(\Session::get('seller')->article_category);
            array_push($ids, \Session::get('seller')->article_category);
        }
        if(!$ids){
            \View::set_global($params);
            $this->template->content = \View::forge("ace/article/index");
            return;
        }
        $items = \Model_Node::query()
            ->related(array('author', 'category'))
            ->where(array(array('category_id', 'IN', $ids), 'is_delete' => 0));

        if(\Input::get('start_at', false)){
            $items->where('created_at', 'between', array(strtotime(\Input::get('start_at')), strtotime(\Input::get('end_at'))));
        }

        //分页查询
        $count = $items->count();
        $config = array(
            'pagination_url' => "/admin/article?{$params_string}",
            'total_items'    => $count,
            'per_page'       => \Input::get('count', 10),
            'uri_segment'    => 'start',
            'show_first'     => true,
            'show_last'      => true,
            'name'           => 'bootstrap3_cn' . (\Input::is_ajax() ? '_ajax' : '')
        );

        $pagination = new \Pagination($config);
        $params['pagination'] = $pagination;        
        $params['items'] = $items
                            ->rows_offset($pagination->offset)
                            ->rows_limit($pagination->per_page)
                            ->get();
        if(\Input::is_ajax()){
            $items = array();
            foreach ($params['items'] as $key => $value) {
                $item = $value->to_array();
                $item['created_at'] = date('Y-m-d H:i:s', $value->created_at);
                $item['author'] = $value->author->to_array();
                $item['category'] = $value->category->to_array();
                array_push($items, $item);
            }
            die(json_encode(array('status' => 'succ', 'msg' => '', 'errcode' => 0, 'data' => $items, 'pagination' => htmlspecialchars_decode($pagination))));
        }

        \View::set_global($params);
        $this->template->content = \View::forge("ace/article/index");
    }

  	/**
  	* 新增一条文章
  	*
    * @param $id 文章ID
  	* @return
  	*/
  	public function action_save($id = 0){
        $params = array();

        $title = ($id ? "编辑" : "新增");
        switch (\Input::get('action', 'article')) {
            case 'article':
                $params['controller_name'] = '文章管理';
                $params['title'] = $title . '文章';
                $params['menu'] = 'article-details';
                break;
            case 'page':
                $params['controller_name'] = '单页管理';
                $params['title'] = $title . '单页';
                $params['menu'] = 'page-details';
                break;
            case 'case':
                $params['controller_name'] = '案例管理';
                $params['title'] = $title . '案例';
                $params['menu'] = 'case-details';
                break;
            case 'ad':
                $params['controller_name'] = '广告管理';
                $params['title'] = $title . '广告';
                $params['menu'] = 'ad-details';
                break;
        }
        $params['action_name'] = $params['title'];

        $node = false;
        if($id){
            $node = \Model_Node::find($id);
        }

    	if(\Input::method() == 'POST'){
            $data = \Input::post();
            if( ! $data['title'] || ! $data['body']){
                \Session::set_flash('msg', array('status' => 'err', 'msg' => '请填写标题及内容', 'errcode' => 10));
            }else{
                if($node){
                    $node->set($data);
                }else{
                    $data['status'] = 0;
                    $data['is_delete'] = 0;
                    $data['user_ip'] = \Input::real_ip();
                    $data['user_agent'] = \Input::user_agent();
                    $node = \Model_Node::forge($data);
                }

                if($node->save()){
                    if(\Input::is_ajax()){
                        die(json_encode(array('status' => 'succ', 'msg' => '操作成功', 'errcode' => 0)));
                    }
                    \Session::set_flash('msg', array('status' => 'succ', 'msg' => '操作成功', 'errcode' => 0));
                }else{
                    if(\Input::is_ajax()){
                        die(json_encode(array('status' => 'err', 'msg' => '操作失败', 'errcode' => 20)));
                    }
                    \Session::set_flash('msg', array('status' => 'err', 'msg' => '操作失败', 'errcode' => 20));
                }
            }
            
    	}

        if($node){
            $params['item'] = $node;
        }

        \View::set_global($params);
        $this->template->content = \View::forge("ace/article/details");
  	}

    /**
    * 文章类目
    *
    * @param $id 文章类目ID
    * @return
    */
    public function action_category($id = 0){
        $params = array('title' => '分类列表',);

        $title = ($id ? "编辑类目" : "新增类目");
        switch (\Input::get('action', 'article')) {
            case 'article':
                $params['controller_name'] = '文章管理';
                $params['menu'] = 'article-category';
                break;
            case 'page':
                $params['controller_name'] = '页面管理';
                $params['menu'] = 'page-category';
                break;
            case 'case':
                $params['controller_name'] = '案例管理';
                $params['menu'] = 'case-category';
                break;
            case 'ad':
                $params['controller_name'] = '广告管理';
                $params['menu'] = 'ad-category';
                break;
        }
        $params['action_name'] = $params['title'];

        $seller = \Session::get('seller');

        $params_string = '?';

        if(\Input::get('action', false) == 'page'){
            $ids = \Model_Category::find($seller->page_category);
            $params_string = 'action=page';
            $params['parent_id'] = $seller->page_category;//分类ID
        }else if(\Input::get('action', false) == 'case'){
            $ids = \Model_Category::find($seller->case_category);
            $params_string = 'action=case';
            $params['parent_id'] = $seller->case_category;//分类ID
        }else if(\Input::get('action', false) == 'ad'){
            $ids = \Model_Category::find($seller->ad_category);
            $params_string = 'action=ad';
            $params['parent_id'] = $seller->ad_category;//分类ID
        }else if(\Input::get('category_id', false)){
            $ids = \Model_Category::find(\Input::get('category_id'));
            array_push($ids, \Input::get('category_id'));
            $params_string = 'category_id=' . \Input::get('category_id');
        }else{
            $ids = \Model_Category::find($seller->article_category);
            $params['parent_id'] = $seller->article_category;//分类ID
        }

        $items = array();

        if($ids){
            $category = $ids->children()->get();//一级子分类
            foreach ($category as $key => $value) {
                    $item = array();
                    $item_child = array();
                    $item = $value;
                    $item_child = $value->children()->get();
                    $item['category'] = $item_child ? $item_child : array();
                    array_push($items, $item);
            }
        }

        $params['items'] = $items;

        \View::set_global($params);
        $this->template->content = \View::forge("ace/article/category");
    }

  	/**
  	* 删除一条文章
  	*
  	* @param $id int 文章ID
  	* @return
  	*/
  	public function action_delete($id){
        if( ! $id){
            die('无效的参数');
        }

        $node = \Model_Node::find($id);
        if( ! $node){
            die('无效的参数');
        }

        //$node->is_delete = 1;
        $goods = \Model_Goods::find($id);
        if($goods){
            $goods->delete();
        }

        $case = \Model_Case::find($id);
        if($case){
            $case->delete();
        }
        
        if($node->delete()){
            if(\Input::is_ajax()){
                die(json_encode(array('stauts' => 'succ', 'msg' => '操作成功', 'errcode' => 0)));
            }
            if($node->category_id == 1){
                \Response::redirect("/admin/node/");
            }else{
                \Response::redirect("/admin/node/list/teacher");
            }
        }
        if(\Input::is_ajax()){
            die(json_encode(array('stauts' => 'err', 'msg' => '操作失败', 'errcode' => 20)));
        }
  	}

}
